package com.compomics.prioritizer.general.gui;

import com.compomics.prioritizer.db.accessors.*;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Vector;
import java.util.HashMap;
import java.sql.SQLException;
import java.sql.Connection;

/**
 * Created by IntelliJ IDEA.
 * User: Niklaas Colaert
 * Date: 9-nov-2008
 * Time: 10:53:28
 * To change this template use File | Settings | File Templates.
 */
public class CreateProtocolGui extends JFrame {
    private JPanel jpanContent;
    private JPanel projectInfoPanel;
    private JTextField txtTitle;
    private JTextArea txtDescription;
    private JList listVarMod;
    private JButton createProtocolButton;
    private JLabel lblModifcation;
    private JComboBox cmbModification;
    private JButton addButton1;
    private JList listFixMod;
    private JButton addButton;
    private JButton deleteButton;
    private JButton deleteButton1;

    private Connection iConn;

    private boolean iProjectModificationsChanged;
    //the fixed Modifications
    private Vector fixedMods;
    //the variable Modifications
    private Vector varMods;
    //the Modifications
    private Modification[] iModifications;


    public CreateProtocolGui(Connection aConn) {
        super("Create a protocol");
        this.iConn = aConn;
        $$$setupUI$$$();
        addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addFixedMod();
            }
        });
        addButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                addVarMod();
            }
        });
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                deleteFixedMod();
            }
        });
        deleteButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                deleteVarMod();
            }
        });
        createProtocolButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                saveProtocol();
            }
        });
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((screen.width / 5), (screen.height / 10));
        setContentPane(jpanContent);
        this.setSize(600, 450);
        this.setVisible(true);

    }


    public void saveProtocol() {
        HashMap hm = new HashMap(3);
        hm.put(Protocol.NAME, txtTitle.getText());
        hm.put(Protocol.DESCRIPTION, txtDescription.getText());

        Protocol lProtocol = new Protocol(hm);
        try {
            lProtocol.persist(iConn);
            Object[] keys = lProtocol.getGeneratedKeys();
            this.storeModifications(Long.valueOf(keys[0].toString()));
            JOptionPane.showMessageDialog(this, "Protocol saved succesfully", "Succes!", JOptionPane.INFORMATION_MESSAGE);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(this, new String[]{"Unable to store protocol:  ", e.getMessage()}, "Error!", JOptionPane.ERROR_MESSAGE);
        }
    }


    public void storeModifications(long aProjectId) {
        //store fixed modifications
        for (int f = 0; f < fixedMods.size(); f++) {
            HashMap hm = new HashMap(3);
            hm.put(Modification_link.PROJECT, 0);
            hm.put(Modification_link.PROTOCOL, 1);
            //type is 1 for fixed
            hm.put(Modification_link.TYPE, 1);
            hm.put(Modification_link.L_MODIFICATIONID, ((Modification) fixedMods.get(f)).getModificationid());
            hm.put(Modification_link.L_LINKID, aProjectId);

            Modification_link lModification_link = new Modification_link(hm);
            try {
                lModification_link.persist(iConn);
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(this, new String[]{"Unable to store modifications for this protocol:  ", e.getMessage()}, "Error!", JOptionPane.ERROR_MESSAGE);
            }
        }

        //store variable modifications
        for (int f = 0; f < varMods.size(); f++) {
            HashMap hm = new HashMap(3);
            hm.put(Modification_link.PROJECT, 0);
            hm.put(Modification_link.PROTOCOL, 1);
            //type is 0 for variable
            hm.put(Modification_link.TYPE, 0);
            hm.put(Modification_link.L_MODIFICATIONID, ((Modification) varMods.get(f)).getModificationid());
            hm.put(Modification_link.L_LINKID, aProjectId);


            Modification_link lModification_link = new Modification_link(hm);
            try {
                lModification_link.persist(iConn);
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(this, new String[]{"Unable to store modifications for this protocol:  ", e.getMessage()}, "Error!", JOptionPane.ERROR_MESSAGE);
            }
        }


    }

    public void addFixedMod() {
        Modification addMod = (Modification) cmbModification.getSelectedItem();
        fixedMods.add(addMod);
        if (fixedMods.size() > 0) {
            deleteButton.setEnabled(true);
        } else {
            deleteButton.setEnabled(false);
        }
        listFixMod.setSelectedIndex(0);
        listFixMod.updateUI();
        iProjectModificationsChanged = true;
    }

    public void addVarMod() {
        Modification addMod = (Modification) cmbModification.getSelectedItem();
        varMods.add(addMod);
        if (varMods.size() > 0) {
            deleteButton1.setEnabled(true);
        } else {
            deleteButton1.setEnabled(false);
        }

        listVarMod.setSelectedIndex(0);
        listVarMod.updateUI();
        iProjectModificationsChanged = true;
    }

    public void deleteFixedMod() {
        Modification delMod = (Modification) listFixMod.getSelectedValue();
        fixedMods.remove(delMod);
        if (fixedMods.size() > 0) {
            deleteButton.setEnabled(true);
        } else {
            deleteButton.setEnabled(false);
        }
        listFixMod.setSelectedIndex(0);
        listFixMod.updateUI();
        iProjectModificationsChanged = true;
    }

    public void deleteVarMod() {
        Modification delMod = (Modification) listVarMod.getSelectedValue();
        varMods.remove(delMod);
        if (varMods.size() > 0) {
            deleteButton1.setEnabled(true);
        } else {
            deleteButton1.setEnabled(false);
        }
        listVarMod.setSelectedIndex(0);
        listVarMod.updateUI();
        iProjectModificationsChanged = true;
    }


    public void gatherData() {
        this.findModifications();
    }

    /**
     * This method collects all modifications.
     * It fills the 'iModifications' cache.
     */
    private void findModifications() {
        try {
            iModifications = Modification.getAllModifications(iConn);
        } catch (SQLException sqle) {
            JOptionPane.showMessageDialog(this, new String[]{"Unable to find modifications:  ", sqle.getMessage()}, "Error!", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void createUIComponents() {
        this.gatherData();

        varMods = new Vector();
        fixedMods = new Vector();
        listVarMod = new JList(varMods);
        listFixMod = new JList(fixedMods);
        cmbModification = new JComboBox(iModifications);
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        createUIComponents();
        jpanContent = new JPanel();
        jpanContent.setLayout(new GridBagLayout());
        projectInfoPanel = new JPanel();
        projectInfoPanel.setLayout(new GridBagLayout());
        GridBagConstraints gbc;
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 2;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        jpanContent.add(projectInfoPanel, gbc);
        projectInfoPanel.setBorder(BorderFactory.createTitledBorder(null, "Create protocol", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.ABOVE_TOP, new Font(projectInfoPanel.getFont().getName(), projectInfoPanel.getFont().getStyle(), 16)));
        final JLabel label1 = new JLabel();
        label1.setText("Protocol name:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(label1, gbc);
        txtTitle = new JTextField();
        txtTitle.setEditable(true);
        txtTitle.setEnabled(true);
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 0;
        gbc.gridwidth = 5;
        gbc.weightx = 1.0;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(txtTitle, gbc);
        final JScrollPane scrollPane1 = new JScrollPane();
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 1;
        gbc.gridwidth = 5;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(scrollPane1, gbc);
        txtDescription = new JTextArea();
        txtDescription.setColumns(0);
        txtDescription.setEditable(true);
        txtDescription.setEnabled(true);
        txtDescription.setRows(5);
        scrollPane1.setViewportView(txtDescription);
        final JLabel label2 = new JLabel();
        label2.setText("Protocol description:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(label2, gbc);
        final JLabel label3 = new JLabel();
        label3.setText("Variable modifications");
        gbc = new GridBagConstraints();
        gbc.gridx = 5;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(label3, gbc);
        final JScrollPane scrollPane2 = new JScrollPane();
        gbc = new GridBagConstraints();
        gbc.gridx = 5;
        gbc.gridy = 5;
        gbc.gridwidth = 2;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(scrollPane2, gbc);
        scrollPane2.setViewportView(listVarMod);
        createProtocolButton = new JButton();
        createProtocolButton.setText("Create Protocol");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 6;
        gbc.gridwidth = 7;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(createProtocolButton, gbc);
        lblModifcation = new JLabel();
        lblModifcation.setText("Modifications:");
        gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(0, 5, 0, 5);
        projectInfoPanel.add(lblModifcation, gbc);
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 2;
        gbc.gridwidth = 5;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(cmbModification, gbc);
        addButton1 = new JButton();
        addButton1.setText("add");
        gbc = new GridBagConstraints();
        gbc.gridx = 5;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(addButton1, gbc);
        final JScrollPane scrollPane3 = new JScrollPane();
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 5;
        gbc.gridwidth = 2;
        gbc.weightx = 1.0;
        gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(scrollPane3, gbc);
        scrollPane3.setViewportView(listFixMod);
        addButton = new JButton();
        addButton.setText("add");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(addButton, gbc);
        final JLabel label4 = new JLabel();
        label4.setText("Fixed modifications");
        gbc = new GridBagConstraints();
        gbc.gridx = 2;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(label4, gbc);
        deleteButton1 = new JButton();
        deleteButton1.setText("delete");
        gbc = new GridBagConstraints();
        gbc.gridx = 6;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(deleteButton1, gbc);
        deleteButton = new JButton();
        deleteButton.setText("delete");
        gbc = new GridBagConstraints();
        gbc.gridx = 3;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.insets = new Insets(5, 5, 5, 5);
        projectInfoPanel.add(deleteButton, gbc);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return jpanContent;
    }
}
